// pages/user/index.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:"帐号",
    DisplayName:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userName: app.globalData.userInfo.Name,
      DisplayName: app.globalData.userInfo.DisplayName,
    })

    //console.log(this.data.userName);
  },
  /**
   * 退出帐号
   */
  goLogout:function()
  {
    //console.log("Token:" + app.globalData.Token);
    app.globalData.userInfo = null;
    app.globalData.Token = "";
    //console.log("Token 1:" + app.globalData.Token);
    try {
      wx.setStorageSync('Token', "")
    } catch (e) {
    }
    wx.switchTab({
      url: '/pages/home/index'
    })
  },
  /**
   * 扫描二维码
   */
  onScan:function()
  {
    wx.scanCode({
      success: (res) => {
        console.log(res);
        console.log("result:" + res.result);

        var did = res.result;
        var token = app.globalData.Token;
        if (token == null || token == "") return;

        wx.request({
          url: app.globalData.Url + "Api/User/Bind", //仅为示例，并非真实的接口地址
          data: {
            token: token,
            did: did,
            type:1
          },
          header: {'content-type': 'application/json' },
          success: function (rs) {
            console.log(rs.data);
            if (rs.data.result) {
              wx.reLaunch({
                url: '/pages/home/index'
              })
            }
            else {
              wx.showToast({
                title: rs.data.data,
                icon: 'none',
                duration: 1000
              })
            }
          }
        })
      }
    })
  }
})